package com.comet.controller;

import com.comet.domain.Order;
import com.comet.mapper.ShardingMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@RestController
public class ShardingController {

    @Resource
    private ShardingMapper shardingMapper;

    // http://localhost:8888/order-service/addOrder
    @RequestMapping("/addOrder")
    public Object addOrder() {
        for (int i = 0; i < 10; i++) {
            Order order = new Order();
            order.setUserId(Long.valueOf("1000000"+i));
            order.setCourseId(Long.valueOf("2000000"+i));
            order.setCourseName("SpringBoot从入门到精通");
            order.setCourseDesc("玩转SpringBoot，就在这里");
            order.setPrice(new BigDecimal(399.00));
            order.setLogoUrl("/img/course_img/sb.jpg");
            order.setCreateTime(new Date());
            order.setRemark("无备注");
            System.out.println(order.toString());
            shardingMapper.addOrder(order);
        }
        return new HashMap<>().put("Status","Success");
    }

    // http://localhost:8888/order-service/select_one
    @RequestMapping("/select_one")
    public Object SelectCourse() {
        Long orderId = 671297521263316993L;
        Order order = shardingMapper.findOrderOne(orderId);
        return order;
    }

    // http://localhost:8888/order-service/select_all
    @RequestMapping("/select_all")
    public Object SelectCourseAll() {
        List<Order> orderList = shardingMapper.findOrderAll();
        return orderList;
    }

    // http://localhost:8888/order-service/select_in
    @RequestMapping("/select_order_in")
    public Object SelectOrderIn() {
        List<Long> listIn = new ArrayList<>();
        listIn.add(671297520793554945L);
        listIn.add(671297521263316993L);
        List<Order> orderList = shardingMapper.findCourseIn(listIn);
        orderList.forEach(order -> System.out.println(order));
        return orderList;
    }

    // http://localhost:8888/order-service/select_range
    @RequestMapping("/select_order_range")
    public Object SelectOrderRange() {
        Long start = 671297520281849856L;
        Long end = 671481504043372544L;
        List<Order> orderList = shardingMapper.findOrderRange(start, end);
        // orderList.forEach(order -> System.out.println(order));
        return orderList;
    }

}
